package com.test.design.gczms.subject.impl;

import com.test.design.gczms.common.ObjDto;
import com.test.design.gczms.observer.Observer;
import com.test.design.gczms.subject.MySubject;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: design-patternz
 * @description: 具体的主题对象
 * @author: <a href="mailto:zhengjiaming@gtmap.cn">zhengjiaming</a>
 * @create: 2025-03-11 14:40
 **/
public class ConcreteSubject implements MySubject {

    //同步创建观察者
//    private Vector<Observer> vector = new Vector<Observer>();
    //观察者集合
    private List<Observer> observers = new ArrayList<>();
    //状态字段
    private String state;
    //一个对象
    private ObjDto objDto;

    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    //通知所有观察者
    public List<ObjDto> notifyObservers(ObjDto objDto) {
        List<ObjDto> list = new ArrayList();
        for (Observer observer : observers) {
            list.add(observer.update(objDto));
        }
        return list;
    }

    @Override
    public void operation() {
        //自身方法实现逻辑
        System.out.println("被观察者执行");
    }

    public List<Observer> getObservers() {
        return observers;
    }

    public void setObservers(List<Observer> observers) {
        this.observers = observers;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public ObjDto getObjDto() {
        return objDto;
    }

    public void setObjDto(ObjDto objDto) {
        this.objDto = objDto;
    }
}
